home *** CD-ROM | disk | FTP | other *** search
-
- #include "DrawUnicodeString.h"
-
- #if ! TARGET_API_MAC_CARBON
-
- #include <Script.h>
-
- #endif
-
-
- /* MakeThemeATSUIStyle creates a simple ATSUI style record that
- based on the current theme fond that can be used in calls to the
- RenderCFString routine. */
- OSStatus MakeThemeATSUIStyle(ThemeFontID themeFontID, ATSUStyle *theStyle) {
- OSStatus err;
- ATSUStyle localStyle;
- ATSUFontID atsuFont;
- Fixed atsuSize;
- short atsuOrientation, fontFamily, fontSize;
- Str255 fontName;
- Style fontStyle;
- Boolean trueVar = true, falseVar = false;
-
- /* Three parrallel arrays for setting up attributes. */
- ATSUAttributeTag theTags[] = {
- kATSUFontTag, kATSUSizeTag, kATSUVerticalCharacterTag,
- kATSUQDBoldfaceTag, kATSUQDItalicTag, kATSUQDUnderlineTag,
- kATSUQDCondensedTag, kATSUQDExtendedTag
- };
- ByteCount theSizes[] = {
- sizeof(ATSUFontID), sizeof(Fixed), sizeof(UInt16),
- sizeof(Boolean), sizeof(Boolean), sizeof(Boolean),
- sizeof(Boolean), sizeof(Boolean)
- };
- ATSUAttributeValuePtr theValues[] = {
- NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL
- };
-
- /* set up locals */
- localStyle = NULL;
- atsuFont = 0;
- atsuSize = 0x00080000;
- atsuOrientation = kATSUStronglyHorizontal; /* kATSUStronglyVertical */
-
- /* calculate the theme font parameters */
- err = GetThemeFont( themeFontID, smSystemScript,
- fontName, &fontSize, &fontStyle);
- if (err != noErr) goto bail;
- atsuSize = FixRatio(fontSize, 1);
-
- /* set the values array to point to our locals */
- theValues[0] = &atsuFont;
- theValues[1] = &atsuSize;
- theValues[2] = &atsuOrientation;
- theValues[3] = ((fontStyle & bold) != 0 ? &trueVar : &falseVar);
- theValues[4] = ((fontStyle & italic) != 0 ? &trueVar : &falseVar);
- theValues[5] = ((fontStyle & underline) != 0 ? &trueVar : &falseVar);
- theValues[6] = ((fontStyle & condense) != 0 ? &trueVar : &falseVar);
- theValues[7] = ((fontStyle & extend) != 0 ? &trueVar : &falseVar);
-
- /* calculate the font ID */
- GetFNum( fontName, &fontFamily);
- err = ATSUFONDtoFontID( fontFamily, fontStyle, &atsuFont);
- if (err != noErr) goto bail;
-
- /* find the font ID */
- err = ATSUFindFontFromName((Ptr)fontName+1, (long)fontName[0],
- kFontFullName, kFontMacintoshPlatform,
- kFontRomanScript, kFontNoLanguage, &atsuFont);
- if (err != noErr) goto bail;
-
- /* create a style */
- err = ATSUCreateStyle(&localStyle);
- if (err != noErr) goto bail;
-
- /* set the style attributes */
- err = ATSUSetAttributes( localStyle, sizeof(theTags)/sizeof(theTags[0]), theTags, theSizes, theValues );
- if (err != noErr) goto bail;
-
- /* store the new style for the caller */
- *theStyle = localStyle;
- return noErr;
- bail:
- if (localStyle != NULL) ATSUDisposeStyle(localStyle);
- return err;
- }
-
-
-
- /* MakeSimpleATSUIStyle creates a simple ATSUI style record that
- can be used in calls to the RenderCFString routine. */
- OSStatus MakeSimpleATSUIStyle(StringPtr fontName, short fontSize, short qdStyle,
- RGBColor *fontColor, ATSUStyle *theStyle) {
- OSStatus err;
- ATSUStyle localStyle;
- ATSUFontID atsuFont;
- Fixed atsuSize;
- short atsuOrientation;
- RGBColor defaultColor = { 0, 0, 0};
- Boolean trueVar = true, falseVar = false;
-
- /* Three parrallel arrays for setting up attributes. */
- ATSUAttributeTag theTags[] = {
- kATSUFontTag, kATSUSizeTag, kATSUVerticalCharacterTag,
- kATSUColorTag, kATSUQDBoldfaceTag, kATSUQDItalicTag,
- kATSUQDUnderlineTag, kATSUQDCondensedTag, kATSUQDExtendedTag
- };
- ByteCount theSizes[] = {
- sizeof(ATSUFontID), sizeof(Fixed), sizeof(UInt16),
- sizeof(RGBColor), sizeof(Boolean), sizeof(Boolean),
- sizeof(Boolean), sizeof(Boolean), sizeof(Boolean)
- };
- ATSUAttributeValuePtr theValues[] = {
- NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
- };
-
- /* set up locals */
- localStyle = NULL;
- atsuFont = 0;
- atsuSize = FixRatio(fontSize, 1);
- atsuOrientation = kATSUStronglyHorizontal; /* kATSUStronglyVertical */
-
- /* set the values array to point to our locals */
- theValues[0] = &atsuFont;
- theValues[1] = &atsuSize;
- theValues[2] = &atsuOrientation;
- theValues[3] = ((fontColor != NULL) ? fontColor : &defaultColor);
- theValues[4] = ((qdStyle & bold) != 0 ? &trueVar : &falseVar);
- theValues[5] = ((qdStyle & italic) != 0 ? &trueVar : &falseVar);
- theValues[6] = ((qdStyle & underline) != 0 ? &trueVar : &falseVar);
- theValues[7] = ((qdStyle & condense) != 0 ? &trueVar : &falseVar);
- theValues[8] = ((qdStyle & extend) != 0 ? &trueVar : &falseVar);
-
- /* find the font ID */
- err = ATSUFindFontFromName((Ptr)fontName+1, (long)fontName[0],
- kFontFullName, kFontMacintoshPlatform, kFontRomanScript,
- kFontNoLanguage, &atsuFont);
- if (err != noErr) goto bail;
-
- /* create a style */
- err = ATSUCreateStyle(&localStyle);
- if (err != noErr) goto bail;
-
- /* set the style attributes */
- err = ATSUSetAttributes( localStyle, (sizeof(theTags) / sizeof(theTags[0])), theTags, theSizes, theValues );
- if (err != noErr) goto bail;
-
- /* store the new style for the caller */
- *theStyle = localStyle;
- return noErr;
- bail:
- if (localStyle != NULL) ATSUDisposeStyle(localStyle);
- return err;
- }
-
-
-
- /* RenderCFString renders a CFString at (h, v) in the current grafport
- using ATSUI using the style specified in the atsui style record. */
- OSStatus RenderUnicodeString(ConstUniCharArrayPtr iText, UniCharCount iTextLength,
- ATSUStyle theStyle, short h, short v, short maxwidth) {
- ATSUTextLayout theLayout = NULL;
- OSStatus err;
-
- /* set up our locals, verify parameters... */
- if (iText == NULL || theStyle == NULL) return paramErr;
- theLayout = NULL;
- if (iTextLength == 0) return noErr;
-
- /* create the ATSUI layout */
- err = ATSUCreateTextLayoutWithTextPtr( iText, 0,
- iTextLength, iTextLength, 1,
- (unsigned long *) &iTextLength, &theStyle, &theLayout);
- if (err != noErr) goto bail;
-
- if (maxwidth != 0) {
- UniCharArrayOffset iLineStart, oLineBreak;
- ATSUTextMeasurement iLineWidth;
- long nlines;
- ATSUTextMeasurement oTextBefore, oTextAfter, oAscent, oDescent;
-
- iLineStart = 0;
- iLineWidth = FixRatio(maxwidth, 1);
- nlines = 0;
- err = ATSUMeasureText( theLayout, 0, iTextLength, &oTextBefore, &oTextAfter, &oAscent, &oDescent);
- if (err != noErr) goto bail;
- do {
- err = ATSUBreakLine( theLayout, iLineStart, iLineWidth, true, &oLineBreak);
- if (err != noErr) goto bail;
- err = ATSUDrawText(theLayout, iLineStart, oLineBreak - iLineStart,
- FixRatio(h, 1),
- FixRatio(v, 1) + FixMul(oAscent + oDescent, FixRatio(nlines++, 1)));
- if (err != noErr) goto bail;
- iLineStart = oLineBreak;
- } while ((oLineBreak < iTextLength) && (nlines < iTextLength));
- } else {
- /* draw the text */
- err = ATSUDrawText(theLayout, 0, iTextLength, FixRatio(h, 1), FixRatio(v, 1));
- if (err != noErr) goto bail;
- }
-
- /* done */
- ATSUDisposeTextLayout(theLayout);
- return noErr;
-
- bail:
- if (theLayout != NULL) ATSUDisposeTextLayout(theLayout);
- return err;
- }
-
-
- /* MeasureUnicodeString returns a rectangle where the CFStringRef would
- be drawn if it were drawn at location (0,0) -- just like QDTextBounds. */
- OSStatus MeasureUnicodeString(ConstUniCharArrayPtr iText, UniCharCount iTextLength,
- ATSUStyle theStyle, short maxwidth, Rect *bounds) {
- ATSUTextLayout theLayout = NULL;
- OSStatus err;
- ATSUTextMeasurement oTextBefore, oTextAfter, oAscent, oDescent;
-
- /* set up our locals, verify parameters... */
- if (iText == NULL || theStyle == NULL) return paramErr;
- theLayout = NULL;
- if (iTextLength == 0) return noErr;
-
- /* create the ATSUI layout */
- err = ATSUCreateTextLayoutWithTextPtr( iText, 0,
- iTextLength, iTextLength, 1,
- (unsigned long *) &iTextLength, &theStyle, &theLayout);
- if (err != noErr) goto bail;
-
- if (maxwidth != 0) {
- UniCharArrayOffset iLineStart, oLineBreak;
- ATSUTextMeasurement iLineWidth;
- long nlines;
- ATSUTextMeasurement oTextBefore, oTextAfter, maxTextAfter, oAscent, oDescent;
-
- iLineStart = 0;
- iLineWidth = FixRatio(maxwidth, 1);
- nlines = 0;
- maxTextAfter = 0;
- do {
- err = ATSUBreakLine( theLayout, iLineStart, iLineWidth, true, &oLineBreak);
- if (err != noErr) goto bail;
- err = ATSUMeasureText( theLayout, iLineStart, oLineBreak - iLineStart,
- &oTextBefore, &oTextAfter, &oAscent, &oDescent);
- if (err != noErr) goto bail;
- nlines++;
- if (maxTextAfter < oTextAfter) maxTextAfter = oTextAfter;
- iLineStart = oLineBreak;
- } while ((oLineBreak < iTextLength) && (nlines < iTextLength));
- SetRect(bounds,
- - FixRound(oTextBefore),
- - FixRound(oAscent),
- FixRound(maxTextAfter),
- FixRound(oDescent + FixMul(oAscent + oDescent, FixRatio(nlines-1, 1))));
- } else {
- /* measure the text */
- err = ATSUMeasureText( theLayout, 0, iTextLength,
- &oTextBefore, &oTextAfter, &oAscent, &oDescent);
- if (err != noErr) goto bail;
- SetRect(bounds,
- - FixRound(oTextBefore),
- - FixRound(oAscent),
- FixRound(oTextAfter),
- FixRound(oDescent)+1);
- }
-
- /* done */
- ATSUDisposeTextLayout(theLayout);
- return noErr;
-
- bail:
- if (theLayout != NULL) ATSUDisposeTextLayout(theLayout);
- return err;
- }
-
-
-
-
- #if TARGET_API_MAC_CARBON
-
-
- /* RenderCFString renders a CFString at (h, v) in the current grafport
- using ATSUI using the style specified in the atsui style record. */
- OSStatus RenderCFString(CFStringRef theString, ATSUStyle theStyle, short h, short v, short maxwidth) {
- CFIndex textLength;
- OSStatus err;
- UniChar *uniBuffer;
- CFRange uniRange;
-
- /* set up our locals, verify parameters... */
- if (theString == NULL || theStyle == NULL) return paramErr;
- uniBuffer = NULL;
- textLength = CFStringGetLength(theString);
- if (textLength == 0) return noErr;
-
- /* get our data */
- uniRange = CFRangeMake(0, textLength);
- uniBuffer = (UniChar *) NewPtr( textLength * sizeof(UniChar) );
- if (uniBuffer == NULL) { err = memFullErr; goto bail; }
- CFStringGetCharacters( theString, uniRange, uniBuffer);
-
- /* render the string */
- err = RenderUnicodeString(uniBuffer, textLength, theStyle, h, v, maxwidth);
- if (err != noErr) goto bail;
-
- /* clean up */
- DisposePtr((Ptr) uniBuffer);
-
- /* leave */
- return noErr;
- bail:
- if (uniBuffer != NULL) DisposePtr((Ptr) uniBuffer);
- return err;
- }
-
-
- /* MeasureCFString returns a rectangle where the CFStringRef would
- be drawn if it were drawn at location (0,0) -- just like QDTextBounds. */
- OSStatus MeasureCFString(CFStringRef theString, ATSUStyle theStyle, short maxwidth, Rect *bounds) {
- CFIndex textLength;
- OSStatus err;
- UniChar *uniBuffer;
- CFRange uniRange;
-
- /* set up our locals, verify parameters... */
- if (theString == NULL || theStyle == NULL) return paramErr;
- uniBuffer = NULL;
- textLength = CFStringGetLength(theString);
- if (textLength == 0) return noErr;
-
- /* get our data */
- uniRange = CFRangeMake(0, textLength);
- uniBuffer = (UniChar *) NewPtr( textLength * sizeof(UniChar) );
- if (uniBuffer == NULL) { err = memFullErr; goto bail; }
- CFStringGetCharacters( theString, uniRange, uniBuffer);
-
- /* render the string */
- err = MeasureUnicodeString(uniBuffer, textLength, theStyle, maxwidth, bounds);
- if (err != noErr) goto bail;
-
- /* clean up */
- DisposePtr((Ptr) uniBuffer);
-
- /* leave */
- return noErr;
- bail:
- if (uniBuffer != NULL) DisposePtr((Ptr) uniBuffer);
- return err;
- }
-
-
- #endif
-